$('div.btn-group-vertical > a.btn-default').click(function () {
    if ($(this).hasClass('btn-primary')) {
        $(this).removeClass('btn-primary');
        $(this).addClass('btn-default');
    }
    else {
        $(this).siblings().removeClass('btn-primary');
        $(this).removeClass('btn-default');
        $(this).addClass('btn-primary');
    }
})
$("body").on("click", ".btn-group-sub>a", function () {
    if (!$(this).hasClass("rectangle-top")) {
        $(this).siblings('a').removeClass('rectangle-top');
        $(this).addClass('rectangle-top');
    }
});
$("body").on("click", "ul.navinnav >li>a", function () {
    if (!$(this).hasClass("select")) {
        var ul = $(this).parent().parent();
        ul.find("a").removeClass('select');
        $(this).addClass('select');
    }
});
$("body").on("click", "ul.navasp >li>a", function () {
    if ($(this).children("p").children("i").hasClass("fa-plus")) {
        $(this).children("p").children("i").removeClass("fa-plus").addClass("fa-minus");
    }
    else if ($(this).children("p").children("i").hasClass("fa-minus")) {
        $(this).children("p").children("i").removeClass("fa-minus").addClass("fa-plus");
    }
    else if ($(this).children("p").children("i").hasClass("fa-caret-right")) {
        $(this).children("p").children("i").removeClass("fa-caret-right").addClass("fa-caret-down");
    }
    else if ($(this).children("p").children("i").hasClass("fa-caret-down")) {
        $(this).children("p").children("i").removeClass("fa-caret-down").addClass("fa-caret-right");
    }
});
$("body").on("click", "ul.navinnav >li>a", function () {
    if ($(this).children("p").children("i").hasClass("fa-plus")) {
        $(this).children("p").children("i").removeClass("fa-plus").addClass("fa-minus");
    }
    else if ($(this).children("p").children("i").hasClass("fa-minus")) {
        $(this).children("p").children("i").removeClass("fa-minus").addClass("fa-plus");
    }
    else if ($(this).children("p").children("i").hasClass("fa-caret-right")) {
        $(this).children("p").children("i").removeClass("fa-caret-right").addClass("fa-caret-down");
    }
    else if ($(this).children("p").children("i").hasClass("fa-caret-down")) {
        $(this).children("p").children("i").removeClass("fa-caret-down").addClass("fa-caret-right");
    }
});
//初始化地图对象，加载地图
var map = new AMap.Map("container", {
    resizeEnable: true,
    center: [119.556673, 32.437979],
    zoom: 14
});
var graspRoad = new AMap.GraspRoad()
var polyline = new AMap.Polyline({
    isOutline: true,
    outlineColor: '#ffeeff',
    borderWeight: 3,
    strokeColor: "#696969",
    strokeOpacity: 1,
    strokeWeight: 6,
    // 折线样式还支持 'dashed'
    strokeStyle: "solid",
    // strokeStyle是dashed时有效
    strokeDasharray: [10, 5],
    lineJoin: 'round',
    lineCap: 'round',
    zIndex: 50,
})
var va=new Vue({
    el:"#tab",
    data:{
        areaList:{areaList:[],carList:[]},
        time:{startTime:"",endTime:""},
        device:{location:[],marker:null}
    },
    methods:{
        selectProjectList:function (data) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/project/list',
                contentType: "application/json",
                data: data,
                success: function(r){
                    if(r.code === 0){
                            va.areaList.areaList=r.list;
                            va.$nextTick(function () {
                                $('.collapse').collapse('hide');
                            })
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        selectCarList:function (data) {
            $.ajax({
                type: "GET",
                url:  baseURL + 'xinpai/car/select',
                contentType: "application/json",
                data: data,
                success: function(r){
                    if(r.code === 0){
                        va.areaList.carList=r.list;
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        show(imei){
            let startTime=$("#startTime").val();
            let endTime=$("#endTime").val();
            if(!isBlank(imei)&&!isBlank(startTime)&&!isBlank(endTime)){
                $.ajax({
                    type: "GET",
                    url:  baseURL + 'xinpai/cardata/select',
                    contentType: "application/json",
                    data:{imei,startTime,endTime},
                    success: function(r){
                        if(r.code === 0){
                            va.device={location:[],marker:null};
                            let pathParam=[];
                            let reLocat=null;
                            for (var j = 0; j < r.list.length; j++) {
                                var obj = r.list[j];
                                let locat=JSON.parse(obj.dataContent);
                                delete locat.state;
                                locat.ag=parseFloat(locat.ag);
                                if(j==0){
                                    pathParam.push(locat);
                                }else{
                                    locat.tm=locat.tm-reLocat.tm;
                                    pathParam.push(locat);
                                }
                                va.device.location.push([locat.x,locat.y])
                                reLocat=jQuery.extend(true, {},JSON.parse(obj.dataContent));
                            }
                            if(r.list.length>0){
                                graspRoad.driving(pathParam,function(error,result){
                                    console.log(result,error);
                                    if(!error){
                                        var path2 = [];
                                        var newPath = result.data.points;
                                        for(var i =0;i<newPath.length;i+=1){
                                            path2.push([newPath[i].x,newPath[i].y])
                                        }
                                        var newLine = new AMap.Polyline({
                                            path:path2,
                                            strokeWeight:6,
                                            strokeOpacity:0.8,
                                            strokeColor:"red",
                                            showDir:true
                                        })
                                        newLine.setMap(map);
                                        map.setFitView()
                                    }
                                })
                                polyline.setPath(va.device.location);
                                polyline.setMap(map);
                                map.setFitView();
                                //map.panTo(new AMap.LngLat(va.device.location[0][0],va.device.location[0][1]));
                            }

                        }else{
                            alert(r.msg);
                        }
                    }
                });
            }else{
                alert("请选择时间");
            }

        }
    },
    created(){
        this.selectProjectList({parentId:GetRequest().pid,type:1});
        this.selectCarList({nexus:'true',project_id:GetRequest().pid});
        //十分秒年月日范围，限制开始结束相隔天数小于30天

    }
})
